package com.philips.work.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.philips.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.philips.common.log.annotation.Log;
import com.philips.common.log.enums.BusinessType;
import com.philips.common.security.annotation.RequiresPermissions;
import com.philips.work.domain.YwDict;
import com.philips.work.service.IYwDictService;
import com.philips.common.core.web.controller.BaseController;
import com.philips.common.core.web.domain.AjaxResult;
import com.philips.common.core.utils.poi.ExcelUtil;
import com.philips.common.core.web.page.TableDataInfo;

/**
 * 字典Controller
 * 
 * @author philips
 * @date 2025-05-19
 */
@RestController
@RequestMapping("/ywDict")
public class YwDictController extends BaseController
{
    @Autowired
    private IYwDictService ywDictService;

    /**
     * 查询字典列表
     */
    @RequiresPermissions("work:ywDict:list")
    @GetMapping("/list")
    public TableDataInfo list(YwDict ywDict)
    {
        startPage();
        List<YwDict> list = ywDictService.selectYwDictList(ywDict);
        return getDataTable(list);
    }

    /**
     * 查询字典列表
     */
    @GetMapping("/findYwDictList/{dictType}")
    public AjaxResult findYwDictList(@PathVariable("dictType") String dictType)
    {
        List<YwDict> list = ywDictService.findYwDictList(dictType);
        if (StringUtils.isNull(list))
        {
            list = new ArrayList<YwDict>();
        }
        return success(list);
    }

    @GetMapping("/getByName/{dictName}")
    public AjaxResult getByName(@PathVariable("dictName") String dictName)
    {
        YwDict ywDict = ywDictService.getByName(dictName);
        if (StringUtils.isNull(ywDict))
        {
            ywDict = new YwDict();
        }
        return success(ywDict);
    }

    /**
     * 导出字典列表
     */
    @RequiresPermissions("work:ywDict:export")
    @Log(title = "字典", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YwDict ywDict)
    {
        List<YwDict> list = ywDictService.selectYwDictList(ywDict);
        ExcelUtil<YwDict> util = new ExcelUtil<YwDict>(YwDict.class);
        util.exportExcel(response, list, "字典数据");
    }

    /**
     * 获取字典详细信息
     */
    @RequiresPermissions("work:ywDict:query")
    @GetMapping(value = "/{ids}")
    public AjaxResult getInfo(@PathVariable("ids") String ids)
    {
        return success(ywDictService.selectYwDictByIds(ids));
    }

    /**
     * 新增字典
     */
    @RequiresPermissions("work:ywDict:add")
    @Log(title = "字典", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YwDict ywDict)
    {
        return toAjax(ywDictService.insertYwDict(ywDict));
    }

    /**
     * 修改字典
     */
    @RequiresPermissions("work:ywDict:edit")
    @Log(title = "字典", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YwDict ywDict)
    {
        return toAjax(ywDictService.updateYwDict(ywDict));
    }

    /**
     * 删除字典
     */
    @RequiresPermissions("work:ywDict:remove")
    @Log(title = "字典", businessType = BusinessType.DELETE)
	@DeleteMapping("/{idss}")
    public AjaxResult remove(@PathVariable String[] idss)
    {
        return toAjax(ywDictService.deleteYwDictByIdss(idss));
    }
}
